package com.hspedu.object;

/**
 * @Author Stringzhua
 * @Date 2024/3/3 9:58
 * description:判断两个Person对象的内容是否相等
 * 如果两个Person对象的各个属性都一样,则返回true,反之,返回false
 */
public class EqualsExercise01 {
    public static void main(String[] args) {
        Person person1 = new Person("jack", 10, '男');
        Person person2 = new Person("jack", 10, '男');

//        System.out.println(person1.equals(person2));//false 这里默认还是比较两个对象的属性
        System.out.println(person1.equals(person2));//true 这里重写了Object的equals方法
    }
}

class Person {//extends Object
    private String name;
    private int age;
    private char gender;

    //重写Object的equals()方法
    public boolean equals(Object obj) {
        //判断如果比较的两个对象是同一个对象,则直接返回true
        if (this == obj) {
            return true;
        }
        //类型的判断
        if (obj instanceof Person) {//类型是Person再进行比较
            //进行类型转换
            //向下转型 Object-->Person
            //因为需要得到obj的各个属性
            Person p = (Person) obj;
            return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender;
        }
        //如果不是Person,则直接返回false
        return false;
    }

    public Person(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}